#******************************************************************
#
# Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os
import subprocess
import SCons.Util

Import('env')

src_dir = env.get('SRC_DIR')
target_os = env.get('TARGET_OS')

cborDir = os.path.join(src_dir, 'extlibs', 'tinycbor', 'tinycbor')
cborRevision = 'v0.5.1'

if not os.path.exists(cborDir):
    msg = '''
*********************************** Error: ************************************
* Please download cbor using the following command:
*   $ git clone https://github.com/intel/tinycbor.git extlibs/tinycbor/tinycbor -b %s
*******************************************************************************
''' % cborRevision
    Exit(msg)

start_dir = os.getcwd()
os.chdir(cborDir)

# Tizen uses a separate process to sync to a particular revision of TinyCBOR.
# Make sure tinycbor_revision stays in sync with extlibs/tinycbor/prep.sh.
# This code also assumes tinycbor_revision is a tag; if it changes to a branch
# or an arbitrary commit, disable this check below.
if target_os != 'tizen' and os.path.exists('.git/HEAD'):
    out = subprocess.check_output('git tag -l ' + cborRevision, shell=True).rstrip()
    if cborRevision not in SCons.Util.to_String(out):
        print(out)
        msg = '''
*********************************** Error: ************************************
* Your TinyCBOR repo is not up to date with the version required by iotivity.
* Please update with the following commands:
*     $ cd extlibs/tinycbor/tinycbor
*     $ git fetch
*******************************************************************************
'''
        Exit(msg)

# Point TinyCBOR repo to desired release tag
if os.path.exists('.git/HEAD'):
    cmd = 'git reset --hard ' + cborRevision
    os.system(cmd)
else:
    print('Assume TinyCBOR is on tag ' + cborRevision)

os.chdir(start_dir)

cbor_src = [
    os.path.join(cborDir, 'src/cborparser.c'),
    os.path.join(cborDir, 'src/cborparser_dup_string.c'),
    os.path.join(cborDir, 'src/cborencoder.c'),
    os.path.join(cborDir, 'src/cborerrorstrings.c'),
]

env['cbor_files'] = cbor_src
env.AppendUnique(CPPPATH=[os.path.join(cborDir, 'src')])
